package com.example.expandview;

import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.expandview.view.ExpandableLayout;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.BlurTransformation;

public class MainActivity extends AppCompatActivity {

    private Button btnHoriztal;
    private ExpandableLayout expandableLayout;
    private RecyclerView rvHoriztal;
    private RecyclerView rvVetical;
    private List<String> contacts = new ArrayList<>();
    private List<String> months = new ArrayList<>();
    private MainAdapter mAdapter;
    private SimpleAdapter simpleAdapter;
    private TextView ivAnima;
    private TextView ivAnimaa;
    private ImageView ivImage;
    private ImageView ivImage2;

//    private String url ="http://192.168.31.169:9000?filePath=/storage/emulated/0/Android/data/cn.app.hscserver/Hsc/thumanils/picthumanil/104928717IMG_1411.jpg";
    private String url ="http://192.168.31.169:9000/?filePath=/storage/emulated/0/Android/data/cn.app.hscserver/Hsc/thumanils/picthumanil/105029740IMG_5999.JPG";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        ivAnima = findViewById(R.id.iv_anima);
        ivAnimaa = findViewById(R.id.iv_animaa);
        ivImage = findViewById(R.id.iviv);
        ivImage2 = findViewById(R.id.iviv2);
        initHoriztalView();
        initVeticalView();


        Glide.with(this)
                .load(url)
                .crossFade(1000)
                .bitmapTransform(new BlurTransformation(this,22))  // “23”：设置模糊度(在0.0到25.0之间)，默认”25";"4":图片缩放比例,默认“1”。
                .into(ivImage);
        Glide.with(this)
                .load(url)
                .into(ivImage2);


    }

    private void initVeticalView() {
        rvVetical = findViewById(R.id.rv_vetical);


        /**竖向**/
        rvVetical.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        rvVetical.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        simpleAdapter = new SimpleAdapter(this, months);
        rvVetical.setAdapter(simpleAdapter);
        int resId = R.anim.layout_animation_fall_down;
        LayoutAnimationController animation = AnimationUtils.loadLayoutAnimation(this, resId);
        rvVetical.setLayoutAnimation(animation);
    }

    private void initHoriztalView() {
        btnHoriztal = findViewById(R.id.btn_horiztal);
        expandableLayout = findViewById(R.id.expandable_layout);
        rvHoriztal = findViewById(R.id.rv_horiztal);

        /***横向***/
        rvHoriztal.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        rvHoriztal.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL));
        mAdapter = new MainAdapter(this, contacts);
        rvHoriztal.setAdapter(mAdapter);
        btnHoriztal.setOnClickListener(new View.OnClickListener() {

            private ValueAnimator valueAnimator;

            @Override
            public void onClick(View v) {
                expandableLayout.toggle();

                //可以传入n个Int类型的数值，这里仅传入两个0、400
                valueAnimator = ValueAnimator.ofInt(0, 400);
                valueAnimator.setDuration(3000);
                //监听3000ms内，从0->400数值的实时变化
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        int curValue = (int) animation.getAnimatedValue();
                        //没有layout的调用，view是不会移动的，layout设置view的位置
                        Log.d("kunP"," 动画-1111111111-------- "+ valueAnimator.getAnimatedValue());
                        ivAnima.layout(curValue, curValue, curValue + ivAnima.getWidth(), curValue + ivAnima.getHeight());
                    }});
//开始动画
                valueAnimator.start();


                Animation translateAnimation = new TranslateAnimation(0,500,0,500);
                // 步骤2：创建平移动画的对象：平移动画对应的Animation子类为TranslateAnimation
                // 参数分别是：
                // 1. fromXDelta ：视图在水平方向x 移动的起始值
                // 2. toXDelta ：视图在水平方向x 移动的结束值
                // 3. fromYDelta ：视图在竖直方向y 移动的起始值
                // 4. toYDelta：视图在竖直方向y 移动的结束值

                translateAnimation.setDuration(3000);
                // 固定属性的设置都是在其属性前加“set”，如setDuration（）
                ivAnimaa.startAnimation(translateAnimation);


            }
        });

    }

    private void initData() {
        contacts.add("1");
        contacts.add("2");
        contacts.add("3");
        contacts.add("4");
        contacts.add("5");
        contacts.add("6");
        contacts.add("7");
        contacts.add("8");
        contacts.add("9");


        months.add("2019年");
        months.add("2018年");
        months.add("2017年");
        months.add("2016年");
        months.add("2015年");
        months.add("2014年");
        months.add("2013年");
        months.add("2012年");
        months.add("2011年");
        months.add("2010年");
        months.add("2009年");
        months.add("2008年");


    }
}
